package com.tzl.design_pattern.observer_pattern;

/**
 * 观察者类,实现了观察者接口
 *
 * @author Zhilei Tang
 * @date Created in 2020/11/14 22:01
 */
public class WeatherDisplay implements Observer {

  private Subject subject;
  private float temperature;
  private float humidity;
  private float pressure;

  /**
   * 注册监听对象
   *
   * @param subject
   */
  public WeatherDisplay(Subject subject) {
    this.subject = subject;
    subject.registerObserver(this);
  }

  @Override
  public void update(float temperature, float humidity, float pressure) {
    this.temperature = temperature;
    this.humidity = humidity;
    this.pressure = pressure;
  }

  public void display() {
    System.out.printf("当前的最新温度为%.2f,湿度为%.2f,气压为%.2f\n", temperature, humidity, pressure);
  }
}
